jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard", //the full name of the class to create
        jsx3.lang.Object, //  extends
        [tibco.admin.monitormanage.framework.mainPage.MainPageInterface, tibco.admin.plugin.PluginContainerInterface], // implements
        function (plugin) {
        	plugin.dashboardScreenId = "tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard.dashboardScreen";

            plugin.instance = null;
            
            plugin.prototype.tabsToLoad = new Object();
            plugin.prototype.allTabs = new Object();
            plugin.prototype.pluginsLoaded = false;
            plugin.prototype.displayScreenWaiting = false;
		    plugin.prototype.screenIdWaiting;
		    plugin.prototype.parameterArrayWaiting;

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard();
                }
                return this.instance;
            }

            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }

            plugin.prototype.getComponentPath = function() {
                return  "tibco/admin/monitormanage/uicomponents/page/dashboard/component.xml"
            }

            plugin.prototype.getTabText = function() {
				return tibco.admin.Main.getServer().getDynamicProperty("@MM@general@DashboardTitle");
            	//return "Dashboard";
            }

            plugin.prototype.getDefaultScreenId = function() {
        		return plugin.dashboardScreenId;
        	}

            plugin.prototype.getScreenIds = function() {
            	return [plugin.dashboardScreenId];
        	}

            plugin.prototype.loadComponent = function(parent) {
	            this.tabsToLoad = new Object();
	            this.allTabs = new Object();
	            this.pluginsLoaded = false;
	            this.displayScreenWaiting = false;
			    this.screenIdWaiting = null;
			    this.parameterArrayWaiting = null;
            
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/dashboard/components/dashboard.xml", this, this.componentLoaded);
            }

            plugin.prototype.componentLoaded = function(objEvent) {
                if(objEvent.status == "SUCCESS")
                {
				   this.block = objEvent.obj;
				   var server = tibco.admin.Main.getServer();
				   tibco.admin.Main.loadResource("tibco/admin/monitormanage/uicomponents/page/dashboard/jss/dashboardJSS.xml", this.getNamespace(), "SPM_DASHBOARD_JSS_CACHE", "jss", true);
				   tibco.admin.Main.loadResource("tibco/admin/monitormanage/uicomponents/page/dashboard/jss/dashboardTextJSS.xml", this.getNamespace(), "SPM_DASHBOARD_TEXT_JSS_CACHE", "ljss", true);

                   var envPane = this.block.getDescendantOfName("environmentPane");
                   if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.dropdown || !tibco.admin.monitormanage.uicomponents.dropdown.environment || !tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment)
                   { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/dropdown/environment/Environment.js", this.getNamespace()); }
                   var environmentsDropdown = tibco.admin.Main.loadUIResource(envPane, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/dropdown/environment/components/environment.xml", true);
                   environmentsDropdown.setUpComponent(true, plugin.dashboardScreenId, "_MM_dshbrd");
                   tibco.admin.monitormanage.general.PollingHandler.register(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID, environmentsDropdown, environmentsDropdown.updateComponent, plugin.dashboardScreenId);
                   tibco.admin.monitormanage.general.RefreshHandler.register(environmentsDropdown, environmentsDropdown.updateComponent, plugin.dashboardScreenId);

                   var timeframePane = this.block.getDescendantOfName("timeframePane");
                   if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.dropdown || !tibco.admin.monitormanage.uicomponents.dropdown.timeframe || !tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe)
                   { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/dropdown/timeframe/Timeframe.js", this.getNamespace()); }
                   var timeframeDropdown = tibco.admin.Main.loadUIResource(timeframePane, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/dropdown/timeframe/components/timeframe.xml", true);
                   timeframeDropdown.setUpComponent("_MM_dshbrd", true);

                   // load the plugins
					var interfaceList = this.getInterfaceList();
					var preloadList = this.getPreloadList();
					this.loadPlugins(interfaceList, preloadList);

				   this.block.repaint();
                }
                else
                {
                   jsx3.log(objEvent.message);
                   if(objEvent.exception)
                   {
                      jsx3.log(objEvent.exception.printStackTrace());
                   }
                }

            }

            plugin.prototype.displayScreen = function(screenIdVal, parameterArray) {
				if(screenIdVal == plugin.dashboardScreenId)
				{
					if(this.pluginsLoaded)
					{
						// notify the components that they need to display
						var message = {screenId:screenIdVal};
						tibco.admin.monitormanage.general.PageBusUtils.publish(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, message);
						
						// notify the plugin components that they need to display
						var iter = this.getPluginList().iterator();
	    	           	while(iter.hasNext()) 
	    	           	{
	        	       	    var obj = iter.next();
	                	    obj.updateComponent();
	                	}
					}
					else
		        	{
		        		this.displayScreenWaiting = true;
		        		this.screenIdWaiting = screenIdVal;
		        		this.parameterArrayWaiting = parameterArray;
		        	}
				}
        	}
        	
        	plugin.prototype.getPreloadList = function() {
        		// preload the tabs
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.page || !tibco.admin.monitormanage.uicomponents.page.dashboard || !tibco.admin.monitormanage.uicomponents.page.dashboard.tabContent || !tibco.admin.monitormanage.uicomponents.page.dashboard.tabContent.serviceSystemMetrics || !tibco.admin.monitormanage.uicomponents.page.dashboard.tabContent.serviceSystemMetrics.ServiceSystemMetrics)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/page/dashboard/tabContent/serviceSystemMetrics/ServiceSystemMetrics.js", this.getNamespace()); }
                    
                var preloadList = new jsx3.util.List();
                preloadList.add(tibco.admin.monitormanage.uicomponents.page.dashboard.tabContent.serviceSystemMetrics.ServiceSystemMetrics);
                return preloadList;
        	}
            
            plugin.prototype.getInterfaceList = function() {
        		if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.page || !tibco.admin.monitormanage.uicomponents.page.dashboard || !tibco.admin.monitormanage.uicomponents.page.dashboard.DashboardHeaderInterface)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/page/dashboard/DashboardHeaderInterface.js", this.getNamespace()); }
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.page || !tibco.admin.monitormanage.uicomponents.page.dashboard || !tibco.admin.monitormanage.uicomponents.page.dashboard.DashboardTabInterface)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/page/dashboard/DashboardTabInterface.js", this.getNamespace()); }
                
                var interfaceList = new jsx3.util.List();
                interfaceList.add("tibco.admin.monitormanage.uicomponents.page.dashboard.DashboardHeaderInterface");
                interfaceList.add("tibco.admin.monitormanage.uicomponents.page.dashboard.DashboardTabInterface");
                return interfaceList;
        	}
            
            plugin.prototype.setupPlugin = function(obj) {
            }
            
            plugin.prototype.onAfterGetPlugins = function() {
            	var headerClassName = "tibco.admin.monitormanage.uicomponents.page.dashboard.DashboardHeaderInterface";
            	var tabClassName = "tibco.admin.monitormanage.uicomponents.page.dashboard.DashboardTabInterface";
            	var headerPane = this.block.getDescendantOfName("dashboardHeaderPluginPane");
            	var headerPluginFound = false;
            	var iter = this.getPluginList().iterator();
            	var tabPluginList = new jsx3.util.List();
                while(iter.hasNext()) 
                {
                	var obj = iter.next();
                	if(jsx3.Class.forName(headerClassName).isInstance(obj))
                	{
                	   	// construct the parent block for the plugin component
	               		var parentBlock = new jsx3.gui.Block("pluginParentBlock", 0, 0, obj.getPluginComponentWidth(), "100%");
	               		parentBlock.setCSSOverride("float:left");
	               		headerPane.setChild(parentBlock);
                	    
               		   	obj.loadComponent(parentBlock);
               		   	headerPluginFound = true;
               		}
               		else if(jsx3.Class.forName(tabClassName).isInstance(obj))
               		{ tabPluginList.add(obj); }
                }
                if(headerPluginFound)
                { headerPane.repaint(); }
                
                // handle the tab plugin components
                if(tabPluginList.size() > 0)
                {
                	var dashboardContentBlock = this.block.getDescendantOfName("dashboardContentBlock");
                	if(tabPluginList.size() == 1)
                	{ 
                		// if there is only one tab component then don't display the tabs
                		tabPluginList.get(0).loadComponent(dashboardContentBlock); 
                		dashboardContentBlock.repaint();
                	}
                	else
                	{
	                	var tabpane = new com.gix.gui.TabbedPane("dashboardTabbedPane", null, null, "100%", "100%");
	                	dashboardContentBlock.setChild(tabpane);
	                	var iter = tabPluginList.iterator();
	                	var first = true;
                		while(iter.hasNext()) 
                		{
                	    	var obj = iter.next();
                	    	var tabId = "dashboard_tab_" + obj.getTabText();
                	    	this.allTabs[tabId] = obj;
                	    	var tab = new com.gix.gui.Tab(tabId, null, null, null, obj.getTabText());
                	    	tab.subscribe(jsx3.gui.Interactive.EXECUTE,this,this.loadTab);
                	    	tabpane.setChild(tab);
                	    	if(first) {
                	    	    obj.loadComponent(tab.getContentPane());
                	    	    first = false;
                	    	}
                	    	else {
                	    	    this.tabsToLoad[tabId] = obj;
                	    	}
                	    }
                	    dashboardContentBlock.repaint();
                	}
                }
                
                // now check to see if the displayScreen call is waiting for the loaded plugins
				this.pluginsLoaded = true;
				if(this.displayScreenWaiting)
				{ 
					this.displayScreenWaiting = false;
					this.displayScreen(this.screenIdWaiting, this.parameterArrayWaiting); 
				}

            }
            
            plugin.prototype.loadTab = function(objEvent) {
               var tabObj = objEvent.target;
               var tabId = 'dashboard_tab_' + tabObj.getText();
                if(this.tabsToLoad[tabId]) {
                    var obj = this.tabsToLoad[tabId];
                    if(obj) {
                        obj.loadComponent(objEvent.target.getContentPane());
                        delete this.tabsToLoad[tabId];
                    }
                }
                this.allTabs[tabId].tabClicked();
            }

        });